package com.mazaiting.redis.serializer;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.util.Objects;

/**
 * 自定义的 Jackson 序列号器
 * 解决的问题: 字符串保存进 redis 多双引号的问题
 * @author mazaiting
 * @create_time 2022/9/8 20:51
 */
public class CustomizeGenericJackson2JsonRedisSerializer<T> extends Jackson2JsonRedisSerializer<T> {
    public CustomizeGenericJackson2JsonRedisSerializer(Class<T> type) {
        super(type);
    }

    @Override
    public byte[] serialize(Object source) throws SerializationException {
        if (Objects.nonNull(source)) {
            if (source instanceof String || source instanceof Character) {
                return source.toString().getBytes();
            }
        }
        return super.serialize(source);
    }


}
